package seatiger.tomcat.catalina.connector;


import seatiger.tomcat.catalina.Connector;
import seatiger.tomcat.catalina.Context;
import seatiger.tomcat.catalina.Request;
import seatiger.tomcat.catalina.Response;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Map;

@Deprecated
public abstract class RequestBase implements ServletRequest, Request {

    private int contentLength = -1;

    /**
     * 虚拟主机获取域名
     */
    private String serverName;

    /**
     *  和客户端连接的socket
     */
    private Socket socket = null;
    /**
     * 和客户端建立的输入流
     */
    private InputStream input = null;

    private String authorization;

    private String contentType = null;

    protected ServletInputStream stream = null;

    /**
     * 一个Request对象关联一个连接器对象
     */
    private Connector connector;

    private Response response = null;

    /**
     * 请求协议
     */
    protected String protocol = null;
    protected int serverPort = -1;

    protected Context context = null;

    protected String scheme = null;


    protected RequestFacade facade = new RequestFacade(this);

    @Override
    public Socket getSocket() {
        return socket;
    }

    @Override
    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void setStream(InputStream input) {
        this.input = input;
    }

    @Override
    public InputStream getStream() {
        return input;
    }

    @Override
    public void setContentLength(int length) {
        this.contentLength = length;
    }

    @Override
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    @Override
    public String getProtocol() {
        return protocol;
    }

    @Override
    public void setServerPort(int port) {
        this.serverPort = port;
    }

    @Override
    public ServletInputStream createInputStream() throws IOException {
        return new RequestStream(this);
    }

    @Override
    public ServletRequest getRequest() {
        return facade;
    }
    @Override
    public void setAuthorization(String authorization) {
        this.authorization = authorization;
    }

    @Override
    public String getAuthorization() {
        return authorization;
    }

    @Override
    public int getContentLength() {
        return contentLength;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public void setContentType(String type) {
        this.contentType = type;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if(stream == null){
            stream = createInputStream();
        }
        return stream;
    }

    public abstract String getParameter(String name);

    public abstract Enumeration getParameterNames();

    public abstract String[] getParameterValues(String name) ;

    public abstract Map<String,String[]> getParameterMap();

    @Override
    public void setConnector(Connector connector) {
        this.connector = connector;
    }

    @Override
    public Connector getConnector() {
        return connector;
    }

    @Override
    public String getServerName() {
        return serverName;
    }

    @Override
    public void setServerName(String name) {
        this.serverName = name;
    }

    @Override
    public Response getResponse() {
        return response;
    }

    @Override
    public void setResponse(Response response) {
        this.response = response;
    }

    @Override
    public void setContext(Context context) {
        this.context = context;
    }

    @Override
    public Context getContext() {
        return context;
    }

    @Override
    public void recycle() {
        authorization = null;
        contentLength = -1;
        contentType = null;
        context = null;
        input = null;
        protocol = null;
        response = null;
        serverName = null;
        serverPort = -1;
        socket = null;
        stream = null;
    }

    @Override
    public void finishRequest() throws IOException {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }

    @Override
    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    @Override
    public String getScheme() {
        return scheme;
    }

    @Override
    public int getServerPort() {
        return serverPort;
    }

    /**
     * 通过重定向的路径获取RequestDispatcher
     * @param path
     * @return
     */
    @Override
    public abstract RequestDispatcher getRequestDispatcher(String path);
}
